from crawler.extractors import JsonpExtractor
from crawler.fields import JsonField, ContainerField

__all__ = ['MatchesFashionMainExtractor', 'MatchesFashionDetailExtractor']


class MatchesFashionMainInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.props.pageProps.searchResults.data.products[*]')
    spu_id = JsonField(json_selector='$.basicInfo.code')
    brand = JsonField(json_selector='$.basicInfo.designerName')
    title = JsonField(json_selector='$.basicInfo.name')
    gender = JsonField(json_selector='$.basicInfo.gender')
    color = JsonField(json_selector='$.basicInfo.colourCode')
    url = JsonField(json_selector='$.basicInfo.productUrl')
    cost_price = JsonField(json_selector='$.pricing.billing.displayValue')
    real_price = JsonField(json_selector='$.pricing.rrp.displayValue')
    size_stock = JsonField(json_selector='$.sizes[*]', many=True)
    currency = JsonField(json_selector='$.pricing.billing.currency')
    image_url = JsonField(json_selector='$.gallery.images[0].template')
    url_format = JsonField(json_selector='$.gallery.images[0].sequence')


class MatchesFashionDetailInfoExtractor(JsonpExtractor):
    product_des = JsonField(json_selector='$.props.pageProps.product.editorial.description')
    composition = JsonField(json_selector='$.props.pageProps.product.editorial.detailBullets[*]', many=True)


class MatchesFashionMainExtractor:
    @classmethod
    def get_item(cls, response, res):
        return cls._extract(response, res)

    @classmethod
    def _extract(cls, response, res):
        task = response.meta['task']
        sex = task['info']['sex']
        category = task['info']['category']

        info_list = list()
        for item in MatchesFashionMainInfoExtractor.get_items(response, res):
            info_dict = dict()
            info_dict['spu_id'] = item.spu_id
            # 品牌
            info_dict['brand'] = item.brand
            # 商标题
            info_dict['title'] = item.title
            # 品类
            info_dict['category'] = category
            # info_dict['color'] = item.color
            # 性别
            info_dict['sex'] = sex
            # 图集 需要请求详情页
            info_dict['atlas'] = ''
            # 成本价 原价对应公价
            info_dict['cost_price'] = item.cost_price.split('$')[1].replace(',', '')
            # 公价
            info_dict['retail_price'] = item.real_pricee.split('$')[1].replace(',', '') if item.real_price else \
            info_dict['cost_price']
            # 货币单位
            info_dict['currency'] = item.currency

            info_dict['url'] = item.url

            # 商品描述。详情页的
            info_dict['product_des'] = ''
            specifications_list = list()
            url_format = item.url_format
            size_stock = item.size_stock

            # for i in size_stock:
            #     specifications_list.append(i['displayName'])
            #     del i['code']
            #     del i['displayName']
            # info_dict['specifications'] = specifications_list
            stock_ls = list()
            for size in size_stock:
                items = dict()
                items['size'] = size['localisedDisplayName']
                if size['stock'] == 'inStock':
                    items['stock'] = 10
                elif size['stock'] == 'lowStock':
                    items['stock'] = 3
                elif size['stock'] == 'outOfStock':
                    continue
                stock_ls.append(items)
                # size['size'] = size.pop('localisedDisplayName')
                # size['stock'] = size.pop('stock')
                del size['localisedDisplayName']
                del size['displayName']
                del size['code']
            info_dict['size_stock'] = stock_ls
            info_dict['atlas'] = ['http:' + item.image_url.format(SEQUENCE=_, WIDTH=900) for _ in url_format]
            info_list.append(info_dict)

        return info_list


class MatchesFashionDetailExtractor:
    @classmethod
    def get_item(cls, response, res):
        return cls._extract(response, res)

    @classmethod
    def _extract(cls, response, res):
        task = response.meta['task']

        # info_list = list()
        info_dict = dict()
        item = MatchesFashionDetailInfoExtractor.get_item(response, res)

        product_des = item.product_des
        info_dict['product_des'] = product_des
        composition_list = item.composition
        if composition_list:
            for composition in composition_list:
                if 'COMPOSITION' in composition:
                    composition = composition.split(':')[1]
                    info_dict['composition'] = composition
                    break
            for composition in composition_list:
                if 'COLOUR' in composition:
                    color = composition.split(':')[1]
                    info_dict['color'] = color
                    break

        return info_dict
